<?php
/*
 *   This file is part of poat.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 */

function debug($obj) {
	global $debugenabled;
	if ($debugenabled) {
		print_r($obj);
		echo ("\n");
	}
}
	
function dblog($obj) {
	global $dbdebugenabled;
	if ($dbdebugenabled) {
		print_r($obj);
		echo ("\n");
	}
}
	
function maillog($obj) {
	global $maildebugenabled;
	if ($maildebugenabled) {
		print_r($obj);
		echo ("\n");
	}
}

function sendStatUpdateMail($file, $prj) {
	global $loader;
	$u = new user;
	$u->teamid=$file->teamid;
	$u->groupid=GROUP_COORD;
	$u->status=STATUS_ACTIVE;
	$list = $loader->loadListBy($u,$u->listByFilters());

	$uas = new user;
	$uas->userid=$file->userid;
	$loader->get($uas);

	array_push($list,$uas);
			
	$spr = new project;
	$spr->id = $file->subprj;
	$loader->get($spr);
	$ar = array($file->name, $prj->name,$spr->name);
	
	foreach ($list as $key => $obj) {
		include(SYSDIR.'/lang/mail_lang_'.$obj->lang.'.inc');
		$text = createMessage($MAIL['statsupdatedfile'],$ar);
		$subject = $MAIL['statsupdatedfilesub']." [".$file->name."]";
		$headers = 'From: '.SENDER_ADDRESS;
		maillog($subject .'<br>'. $text);
		$r = mail($obj->mail,$subject, $text,$headers);
	}

}
		
function createMessage($string, $array) {
	
	for ($i=0;$i<count($array);$i++) {
		$string = str_replace("&".$i,$array[$i],$string);
	}
	
	return $string;
}

function today() {
	return date('Y-m-d H:i:s',time());
}

function sendLogFile($to,$file) {

	$attach = chunk_split(base64_encode(file_get_contents($file)));
	$hash = md5(time()); 
	
	$filename = "stats".date("Ymdhi",time()).".log";
	
	$headers = "From: POAT-STATS <".SENDER_ADDRESS.">\r\n";
	$headers  .= "MIME-Version: 1.0\r\n";
	$headers  .= "Content-Type: multipart/mixed; ";
	$headers  .= "boundary=".$hash."\r\n";
	$headers  .= "--$hash\r\n";
    $headers .= "Message-ID: <".$hash." TheSystem@".$_SERVER['SERVER_NAME'].">\r\n";
	$headers .= "X-Mailer: PHP v".phpversion()."\r\n";      
    $headers  .= "Content-Type:"."application/octet-stream"." ";
	$headers  .= "name=\"".$filename."\"r\n";
	$headers  .= "Content-Transfer-Encoding: base64\r\n";
	$headers  .= "Content-Disposition: attachment; ";
	$headers  .= "filename=\"".$filename."\"\r\n\n";
	$headers  .= "".$attach."\r\n";
	$headers  .= "--".$hash."--";            		

	$r = mail($to, "Stats Log", "",$headers);

}

?>

